﻿using MediatR;
using SheepCultivation.Common.Domain.Week2;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;

namespace SheepCultivation.API.AddliCation.BuildingD
{
    public class ColumnCreateHandler:IRequestHandler<ColumnCreateCommon,Result>
    {
        #region 仓储
        /// <summary>
        /// 仓储
        /// </summary>
        public readonly IBaseRepository<Building> BuildingRepository;
        public readonly IBaseRepository<BuildingType> BuildingTypeRepository;
        public readonly IBaseRepository<Columns> ColumnsRepository;
        public readonly ILogger<ColumnCreateHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="buildingRepository">栋舍信息</param>
        /// <param name="buildingTypeRepository">栋舍类型</param>
        /// <param name="logger">日志</param>
        public ColumnCreateHandler(IBaseRepository<Building> buildingRepository, IBaseRepository<BuildingType> buildingTypeRepository, IBaseRepository<Columns> ColumnsRepository, ILogger<ColumnCreateHandler> logger)
        {
            BuildingRepository = buildingRepository;
            BuildingTypeRepository = buildingTypeRepository;
            this.logger = logger;
            this.ColumnsRepository = ColumnsRepository;
        }
        #endregion
        #region 栏位新增
        /// <summary>
        /// 栏位新增
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(ColumnCreateCommon request, CancellationToken cancellationToken)
        {
            var result = new Result();
            //异常处理
            try
            {
                logger.LogInformation("栏位新增成功!");
                //判断单添还是批量添加
                if (!string.IsNullOrEmpty(request.columnsName))
                {
                    var list = ColumnsRepository.GetAll().FirstOrDefault(x => x.ColumnsName == request.columnsName && x.BuildingId == request.buildingId);
                    if (list != null)
                    {
                        result.Code = -1;
                        result.Msg = "该栋舍下已经存在该名称的栏位";
                        return result;
                    }
                    else
                    {
                        //实例化栋舍
                        Columns columns = new Columns();
                        columns.ColumnsName = request.columnsName;
                        columns.BuildingId = request.buildingId;
                        result.Code = ColumnsRepository.Add(columns);
                        result.Msg = "栏位新增成功!";
                    }
                }
                else
                {
                    int n = request.getNumber;
                    for (int i = 1; i <= n; i++)
                    {
                        //实例化栋舍
                        Columns columns = new Columns();
                        columns.ColumnsName = request.prefix + request.fieldNumber.ToString();
                        var info = ColumnsRepository.GetAll().FirstOrDefault(x => x.ColumnsName == columns.ColumnsName && x.BuildingId == request.buildingId);
                        if (info != null)
                        {
                            result.Code = -1;
                            result.Msg = "该栋舍里名称为" + columns.ColumnsName + "的栏位已存在,已生成" + (i - 1) + "条数据,往后数据停止生成";
                            return result;
                        }
                        columns.BuildingId = request.buildingId;
                        result.Code += ColumnsRepository.Add(columns);
                        request.fieldNumber++;
                    }
                    result.Msg = n + "个栏位添加成功!";
                }
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("栏位新增失败!" + ex.Message);
                result.Msg = "栏位新增失败";
            }
            return result;
        }
        #endregion
       
    }
}
